<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>登录</title>
    <script type="text/javascript"
            src="${pageContext.request.contextPath }/js/jquery-3.6.0.js"></script>

</head>
<body>
<form >
    用户名：<input type="text" id="username" /><br/>
    密&nbsp;&nbsp;&nbsp;码：<input type="password" id="password"/>
    <br/>
    <input type="button" value="登录" onclick="sumbmit()"/>
</form>

<script type="text/javascript">
    function sumbmit() {
        var username = $("#username").val();
        var password = $("#password").val();
        var data = {username: username, password: password}
        console.log(data)
        $.ajax({
            url: "${pageContext.request .contextPath }/ajax/login",
            type: "post",
            data: data,
            //contentType: "application/x-www-form-urlencoded; charset=UTF-8",
            dataType: "json",
            success: function (response) {
                console.log(response)

                console.log(response.data)
                if (response.code == 0 ) {
                    alert(response.msg)
                }
               // window.location.href="${pageContext.request.contextPath }/ajax/tohome"

            },
            error: function (response) {
                console.log(response)
                alert(response);
            }
        });
    }

    function sumbmitbyJSON() {
        var username = $("#username").val();
        var password = $("#password").val();
        $.ajax({
            url: "${pageContext.request.contextPath }/ajax/loginjson",
            type: "post",
            data: JSON.stringify({username: username, password: password}),
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            success: function (response) {
                console.log(response)
                if (response.code != 0 ) {

                }
                alert(response.msg);
                //window.location.href="${pageContext.request.contextPath }/ajax/tohome"
            },
            error: function (response) {
                console.log(response)

                alert(response.msg);
            }
        });
    }


</script>
</body>
</html>

